﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_PayBackItem")]
    public class P_PayBackItem : IEntity<P_PayBackItem>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string PayBackUser { get; set; }
        public int PayBackID { get; set; }
        public int CycleNumber { get; set; }
        public DateTime PayBackDate { get; set; }
        public decimal PayBackInterest { get; set; }
        public decimal PayBackPrincipal { get; set; }
        public decimal PayBackRemain { get; set; }
        public int Status { get; set; }
        public DateTime PayBackTime { get; set; }
        public int PolicyId { get; set; }
        public int ProjectId { get; set; }

        public override P_PayBackItem Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_PayBackItem Find(System.Linq.Expressions.Expression<Func<P_PayBackItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PayBackItem.FirstOrDefault(where);
                if (item != null)
                {
                    this.CycleNumber = item.CycleNumber;
                    this.ID = item.ID;
                    this.PayBackDate = item.PayBackDate;
                    this.PayBackID = item.PayBackID;
                    this.PayBackInterest = item.PayBackInterest;
                    this.PayBackPrincipal = item.PayBackPrincipal;
                    this.PayBackRemain = item.PayBackRemain;
                    this.PayBackTime = item.PayBackTime;
                    this.PayBackUser = item.PayBackUser;
                    this.Status = item.Status;
                    this.UserName = item.UserName;
                    this.PolicyId = item.PolicyId;
                    this.ProjectId = item.ProjectId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_PayBackItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PayBackItem.Any(where);
            }
        }
    }
}